//
// File name    : DbfConvertor.frmDbfViewModify.cs
// Description  : Review fields converted before uploading
// Author       : Dinh Van Thao (thaobmt204@yahoo.com)
// Date         : 18/07/2011
// Copyright (C) 2011 
// 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Collections;
using System.Runtime.InteropServices;

namespace VNPT.DoiSoat
{
    public partial class frmDbfViewModify : DevExpress.XtraEditors.XtraForm
    {
        public frmDbfViewModify()
        {
            InitializeComponent();
            gridView1.OptionsBehavior.Editable = false;
            LoadDbfFields();
        }

        private void LoadDbfFields()
        {
            StringBuilder strType = new StringBuilder();
            BinaryReader br;
            DataTable Dt = new DataTable();
            Dt.Columns.Add("Field", Type.GetType("System.String"));
            Dt.Columns.Add("Type", Type.GetType("System.String"));

            try
            {
                //cmdAdd.Enabled = (radioGroup1.SelectedIndex == 1 ? true : false);
                //cmdRemove.Enabled = (radioGroup1.SelectedIndex == 1 ? true : false);
                //gridView1.OptionsBehavior.Editable = (radioGroup1.SelectedIndex == 1 ? true : false);

                br = new BinaryReader(File.OpenRead(Dbf.DbfFile));
                byte[] buffer = br.ReadBytes(Marshal.SizeOf(typeof(Dbf.DBFHeader)));

                GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                Dbf.DBFHeader header = (Dbf.DBFHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(Dbf.DBFHeader));
                handle.Free();

                ArrayList fields = new ArrayList();
                while ((13 != br.PeekChar()))
                {
                    buffer = br.ReadBytes(Marshal.SizeOf(typeof(Dbf.FieldDescriptor)));
                    handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                    //fields.Add((Dbf.FieldDescriptor)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(Dbf.FieldDescriptor)));
                    var tem_ = (Dbf.FieldDescriptor)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(Dbf.FieldDescriptor));
                    //if (tem_.FieldName != "_NullFlags")
                    {
                        fields.Add(tem_);
                    }
                    handle.Free();
                }

                foreach (Dbf.FieldDescriptor field in fields)
                {
                    switch (field.FieldType)
                    {
                        //case 'C':
                        case 'C':
                            strType = new StringBuilder("Character(" + field.FieldLen.ToString() + ")");
                            break;
                        case 'N':
                            strType = new StringBuilder("Numeric(" + field.FieldLen.ToString());
                            if (field.Count > 0)
                            {
                                strType.Append(", " + field.Count.ToString() + ")");
                            }
                            else
                            {
                                strType.Append(")");
                            }
                            break;
                        case 'D':
                            strType = new StringBuilder("Date");
                            break;
                    }

                    DataRow dr = Dt.NewRow();
                    dr["Field"] = field.FieldName;
                    dr["Type"] = strType.ToString();

                    Dt.Rows.Add(dr);
                }
                gridControl1.DataSource = Dt;
                br.Close();
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Error: " + ex.Message);
            }
        }
    }
}